package com.teamin.web.socket;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by qfu on 2017/10/27.
 */
public class SessionManager {


   private static Map<String,Set<String>> sessionMap = new ConcurrentHashMap<>() ;

    public static void deleteSessionUser(String userId,String sessionId) {
        Set<String> sessionS = sessionMap.get(userId);
        if(sessionS != null) {
            sessionS.remove(sessionId) ;
            if(sessionS.size() == 0) {
                sessionMap.remove(userId) ;
            }
        }

    }

    /**
     * 用户是否在线
     * @param userId
     * @return
     */
    public static boolean userOnline(String userId) {
        if(!sessionMap.containsKey(userId) || sessionMap.get(userId) == null) {
            return false ;
        }
        return sessionMap.get(userId).size()>0?true:false ;
    }

    public static void addSession(String userId,String sessionId) {
        Set<String> sessionS = sessionMap.get(userId);
        if(sessionS == null) {
            sessionS = new HashSet<>() ;
            sessionMap.put(userId,sessionS) ;
        }
        sessionS.add(sessionId) ;
    }
}
